package context;

import json.IReader;
import json.IWriter;
import json.JacksonReader;
import json.JacksonWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TpContextFactory {

	private static TpContextFactory mInstance = new TpContextFactory();

	private final Logger kLog = LoggerFactory.getLogger(TpContextFactory.class);

	private IReader dbReader;
	private IWriter dbWriter;

	private static final String productsFile = "src/main/resources/json/dbProducts.json";
	private static final String offerProductsFile = "src/main/resources/json/dbOfferProducts.json";
	private static final String offerSellFile = "src/main/resources/json/dbOfferSell.json";
	private static final String sellsFile = "src/main/resources/json/dbSells.json";
	private static final String cuponsFile = "src/main/resources/json/dbCupons.json";

	public void init() {
		mInstance = this;
		try {
			synchronized (mInstance) {
				initialize();
			}
		} catch (Exception e) {
			this.kLog.error("Error initializing ContextFactory. ", e);
		}
	}

	private void initialize() {
		this.kLog.debug("Loading Context");

		this.dbReader = new JacksonReader();
		((JacksonReader) dbReader).setCuponsFile(cuponsFile);
		((JacksonReader) dbReader).setOfferProductsFile(offerProductsFile);
		((JacksonReader) dbReader).setOfferSellFile(offerSellFile);
		((JacksonReader) dbReader).setProductsFile(productsFile);
		((JacksonReader) dbReader).setSellsFile(sellsFile);

		this.dbWriter = new JacksonWriter();
		((JacksonWriter) dbWriter).setCuponFile(cuponsFile);
		((JacksonWriter) dbWriter).setOfferProductsFile(offerProductsFile);
		((JacksonWriter) dbWriter).setOfferSellFile(offerSellFile);
		((JacksonWriter) dbWriter).setProductsFile(productsFile);
		((JacksonWriter) dbWriter).setSellsFile(sellsFile);
	}

	public IReader getDbReader() {
		return dbReader;
	}

	public IWriter getDbWriter() {
		return dbWriter;
	}

}
